package OLD;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;

import com.sun.lwuit.events.*;
import com.sun.lwuit.layouts.GridLayout;
import java.io.IOException;

import com.sun.lwuit.animations.*;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.plaf.UIManager;
import java.util.Vector;

import util.Color;

/**
 *
 * @author nialldeasy
 */
public class Menu {

    int Color_white = 9999999, Color_Black = 0;
    String name;
    String backround = "";
    int transitionType = 0;
    int layoutType = 0;
    Vector buttons = new Vector();
    Vector lists = new Vector();
    public Vector anythingElseToAdd = new Vector();
    Vector commands = new Vector();
    //////////////////////////
    int backMenu = -1;
    public Color color = new Color();
    public boolean isActive;
    ActionListener a;
    public String goTo = "";
    Command exitCommand = new Command("Exit");
    Image Backround;
    String curr_list;
    Form f = null;

    public String getName() {
        return name;
    }

    public void run() {

        f = new Form(name);
        f.setCommandListener(a);
        setTransition(f, 1);
        if (backround != "") {
            try {
                f.setBgImage(Image.createImage("/images/" + backround));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        setLayout(f, layoutType);

        addComponents(f);
        f.show();
    }

    public void delete() {
        f = null;
        System.gc();
    }

    public Form getForm() {
        if (f != null) {
            return f;
        } else {
            return new Form();
        }
    }

    public Menu(String Name, String Backround, ActionListener A) {
        a = A;
        name = Name;
        backround = Backround;
        commands.addElement(exitCommand);
    }

    public Menu(String Name, ActionListener A) {
        a = A;
        name = Name;
        commands.addElement(exitCommand);

    }

    public void update() {
        f.repaint();
    }

    public void setBackCommand(String Name) {
        commands.removeElementAt(0);
        exitCommand = new Command(Name);
        commands.insertElementAt(exitCommand, 0);
    }

    public void addCommand(String Menu_Name) {
        Command c = new Command(Menu_Name);
        commands.addElement(c);

    }

    public void setAction(String ComponentType, String COmponentName) {
    }

    public void addButton(String name, String next, String picture, int textColor) {
        try {
            Button b = new Button(Image.createImage("/images/" + picture + "_unsel.png"));
            b.setUIID(name);
            b.setText(name);
            b.setRolloverIcon(Image.createImage("/images/" + picture + "_sel.png"));
            b.setAlignment(Label.CENTER);
            b.setUIID(next);
            b.setTextPosition(Label.BOTTOM);
            b.setFocusable(true);
            b.addActionListener(a);

            b.setFocusPainted(false);
            b.getStyle().setFgColor(textColor);
            buttons.addElement(b);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public String getFocusedListItem() {

        return (String) (searchList(curr_list).getSelectedItem());
    }

    public void add(Component c) {
        anythingElseToAdd.addElement(c);
    }

    public void addList(List l) {
        l.addActionListener(a);
        l.setListCellRenderer(new TickerRenderer());
        lists.addElement(l);
    }

    public void isReader(Form f, String selected) {
    }

    public List searchList(String s) {
        for (int i = 0; i < lists.size(); i++) {
            String name = ((List) lists.elementAt(i)).getUIID();
            String £name = "£" + s;
            if (£name.equals(name)) {
                return (List) lists.elementAt(i);
            }
        }
        return null;
    }

    public boolean setList(String Name) {
        List l = searchList(Name);
        if (l != null) {
            f.replace(searchList(curr_list), l, null);
            f.repaint();
            curr_list = Name;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 0=None
     * 1=2*10 grid
     * 2=1*10 grid
     */
    public void setLayoutType(int i) {
        layoutType = i;
    }

    /**
     * 0=None
     * 1=slide right
     * 2=slide left
     * 3=fade quickly
     * 4=fade slowly
     */
    public void setTransitionType(int i) {
        transitionType = i;
    }

    ////////////////////////For Menu_Manager use only/////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////
    public void setTransition(Form f, int i) {
        if (i == 0) {
            f.setTransitionOutAnimator(null);
        }
        if (i == 1) {
            f.setTransitionOutAnimator(
                    CommonTransitions.createSlide(
                    CommonTransitions.SLIDE_HORIZONTAL, true, 200));
        }
        if (i == 2) {
            f.setTransitionOutAnimator(
                    CommonTransitions.createSlide(
                    CommonTransitions.SLIDE_HORIZONTAL, false, 200));
        }
        if (i == 3) {
        }
        if (i == 4) {
        }
    }

    public void addComponents(Form f) {
        removeComponents(f);
        for (int i = 0; i < commands.size(); i++) {
            f.addCommand((Command) commands.elementAt(i));
        }
        for (int i = 0; i < this.anythingElseToAdd.size(); i++) {
            f.addComponent((Component) this.anythingElseToAdd.elementAt(i));
        }
        for (int i = 0; i < buttons.size(); i++) {
            f.addComponent((Button) buttons.elementAt(i));
        }
        if (!lists.isEmpty()) {
            curr_list = ((List) lists.elementAt(0)).getUIID().substring(1, ((List) lists.elementAt(0)).getUIID().length());
            f.addComponent((List) lists.elementAt(0));

        }
    }

    public void removeComponents(Form f) {
        for (int i = 0; i < this.anythingElseToAdd.size(); i++) {
            f.removeComponent((Component) this.anythingElseToAdd.elementAt(i));
        }
        for (int i = 0; i < buttons.size(); i++) {
            f.removeComponent((Component) buttons.elementAt(i));
        }
        for (int i = 0; i < lists.size(); i++) {
            f.removeComponent((Component) lists.elementAt(i));
        }

    }

    public void setLayout(Form f, int i) {
        if (i == 0) {
            f.setLayout(new GridLayout(10, 2));
        }
        if (i == 1) {
            f.setLayout(new GridLayout(10, 2));
        }
        if (i == 2) {
            f.setLayout(new GridLayout(10, 1));
        }
        if (i == 3) {
            f.setLayout(new GridLayout(10, 3));
        }
        if (i == 4) {
        }


    }

    class TickerRenderer extends DefaultListCellRenderer {

        private DefaultListCellRenderer selectedRenderer = new DefaultListCellRenderer(false);
        private List parentList;

        public TickerRenderer() {
            super(false);
        }

        public boolean animate() {
            if (parentList != null && parentList.getComponentForm() != null) {
                if (selectedRenderer.isTickerRunning()) {
                    if (selectedRenderer.animate()) {
                        parentList.repaint();
                    }
                }
            }
            return super.animate();
        }

        public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
            if (isSelected) {
                selectedRenderer.getListCellRendererComponent(list, value, index, isSelected);

// sometimes the list asks for a dummy selected value for size calculations and this might
// break the tickering state
                if (index == list.getSelectedIndex()) {
                    if (selectedRenderer.shouldTickerStart()) {
                        if (!selectedRenderer.isTickerRunning()) {
                            parentList = list;
                            list.getComponentForm().registerAnimated(this);
                            selectedRenderer.startTicker(UIManager.getInstance().getLookAndFeel().getTickerSpeed(), true);
                        }
                    } else {
                        if (selectedRenderer.isTickerRunning()) {
                            selectedRenderer.stopTicker();
                        }
                    }
                }
                return selectedRenderer;
            } else {
                return super.getListCellRendererComponent(list, value, index, isSelected);
            }
        }
    }
}

